package chap_02_CAS.step_01_Atomic_Base;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @Description AtomicReference 使用学习
 * @Author shihan
 * @Date 2022/10/29 17:38
 * @Version 1.0
 */
public class Atomic_01_Reference {

    /**
     * 注意：AtomicReference对象类型，比较的是对象的地址指针，而不是对象内的属性数据
     *
     * @param args
     */
    public static void main(String[] args) {
        User z3 = new User("z3", 23);
        User l4 = new User("l4", 24);

        // 原始值是 z3
        AtomicReference<User> reference = new AtomicReference<>(z3);

        // 判断当前对象是 z3，才更新为 l4
        System.out.println(reference.compareAndSet(z3, l4) + "\t" + reference.get().toString());

    }

}

@AllArgsConstructor
@Data
class User {
    private String name;
    private Integer age;
}

